package com.sky.service.impl;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderedMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    OrderedMapper orderedMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO businessData(LocalDate now) {
        //1.查询今日营业额
        BigDecimal turnover = orderedMapper.sumCondition(Orders.COMPLETED, now);
        //2.有效订单数
        Map map = new HashMap<>();
        map.put("date", now);
        Integer orderNow = orderedMapper.getOrderNow(map);//总订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderedMapper.getOrderNow(map);//有效订单数
        double orderCompletionRate = 0.0 ;
        double unitPrice = 0;
        if (validOrderCount != null && validOrderCount!=0 && orderNow != null && orderNow != 0
            && turnover != null) {
            //3.订单完成率 有效订单数 / 总订单数
            orderCompletionRate = validOrderCount.doubleValue() / orderNow;
            //4.新增用户数平均客单价 营业额 / 有效订单数
            unitPrice = turnover.doubleValue()/validOrderCount;
        }
        //5.新增用户数
        Integer newUsers = userMapper.getUserQuery(now);
        //6.封装vo返回
        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(turnover.doubleValue());//营业额
        businessDataVO.setValidOrderCount(validOrderCount);//有效订单数
        businessDataVO.setOrderCompletionRate(orderCompletionRate);//订单完成率
        businessDataVO.setUnitPrice(unitPrice);//平均客单价
        businessDataVO.setNewUsers(newUsers);//新增用户数
        return businessDataVO;
    }

    @Override
    public Map overviewOrders() {
        //查询订单总数
        Map hashMapmap = new HashMap();
        hashMapmap.put("date", LocalDate.now());
        Integer allOrders = orderedMapper.getOrderNow(hashMapmap);
        //查询待接单数量
        hashMapmap.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderedMapper.getOrderNow(hashMapmap);
        //查询待派送数量
        hashMapmap.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderedMapper.getOrderNow(hashMapmap);
        //查询已完成数量
        hashMapmap.put("status", Orders.COMPLETED);
        Integer completedOrders = orderedMapper.getOrderNow(hashMapmap);
        //查询已取消数量
        hashMapmap.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderedMapper.getOrderNow(hashMapmap);
        //封装map返回
        Map map = new HashMap<>();
        map.put("allOrders",allOrders);//全部订单
        map.put("cancelledOrders",cancelledOrders);//已取消数量
        map.put("completedOrders",completedOrders);//已完成数量
        map.put("deliveredOrders",deliveredOrders);//待派送数量
        map.put("waitingOrders",waitingOrders);//待接单数量
        return map;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        //查询启用菜品数量
        Integer Sold = dishMapper.getCount(StatusConstant.ENABLE);
        //查询已停售菜品数量
        Integer discontinued = dishMapper.getCount(StatusConstant.DISABLE);
        //封装vo返回
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(Sold);
        dishOverViewVO.setDiscontinued(discontinued);
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //查询启用套餐数量
       Integer sold = setmealMapper.getCount(StatusConstant.ENABLE);
        //查询停用套餐数量
       Integer discontinued = setmealMapper.getCount(StatusConstant.DISABLE);
        //封装vo返回
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(sold);
        setmealOverViewVO.setDiscontinued(discontinued);
        return setmealOverViewVO;
    }
}
